package cn.yqn.org.service.impl;

import cn.yqn.basic.exception.BusinessException;
import cn.yqn.basic.utils.BaiduAuditUtils;
import cn.yqn.org.domain.Employee;
import cn.yqn.org.domain.Shop;
import cn.yqn.org.domain.ShopAuditLog;
import cn.yqn.org.domain.ShopDto;
import cn.yqn.org.mapper.EmployeeMapper;
import cn.yqn.org.mapper.ShopAuditLogMapper;
import cn.yqn.org.mapper.ShopMapper;
import cn.yqn.org.service.IShopService;
import cn.yqn.basic.service.impl.BaseServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.mail.javamail.JavaMailSender;
import org.springframework.mail.javamail.MimeMessageHelper;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import javax.mail.MessagingException;
import javax.mail.internet.MimeMessage;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author yqn
 * @since 2022-07-24
 */
@Service
public class ShopServiceImpl extends BaseServiceImpl<Shop> implements IShopService {

    @Autowired
    private ShopMapper shopMapper;

    @Autowired
    private EmployeeMapper employeeMapper;

    @Autowired
    private JavaMailSender javaMailSender;

    @Autowired
    private ShopAuditLogMapper shopAuditLogMapper;

    @Override
    // 实现shop入驻流程
    public void settlement(Shop shop) {
        Employee admin = shop.getAdmin();
        // 1. 非空校验,使用spring中的验证方法
        if(StringUtils.isEmpty(shop.getName())
                || StringUtils.isEmpty(shop.getAddress())
                || StringUtils.isEmpty(shop.getTel())
                || StringUtils.isEmpty(admin.getUsername())
                || StringUtils.isEmpty(shop.getAdmin().getPhone())
                || StringUtils.isEmpty(shop.getAdmin().getEmail())
                || StringUtils.isEmpty(shop.getAdmin().getPassword())
                || StringUtils.isEmpty(shop.getAdmin().getComfirmPassword())
        ){
            // 提示店铺商家，参数不能为空
            throw new BusinessException("请输入参数");
        }
        // 2. 密码校验
        if(!admin.getPassword().equals(admin.getComfirmPassword())){
            throw new BusinessException("确认密码不一致，请重新输入！");
        }
        // 百度AI验证店铺名称是否违规
        Boolean boo = BaiduAuditUtils.TextCensor(shop.getName());
        if (!boo) {
            throw new BusinessException("店铺名称存在违规,请输入合法信息!");
        }
        // 3. 店铺注册过校验
        Shop dbShop = shopMapper.getShopByMessage(shop);
        if(dbShop!=null){
            throw new BusinessException("店铺已注册，请更换店铺信息！");
        }
        // 保存两表字段信息
        // 1. 获取自增主键,保存employee
        employeeMapper.save(admin);
        // 2. 把自增的主键设置到shop表中admin_id中
        shop.setAdmin_id(admin.getId());
        // 3. 保存shop数据，返回自增的主键
        shopMapper.save(shop);
        // 4. 3 返回的自增主键设置给employee的shop_id字段
        admin.setShop_id(shop.getId());
        // 5. 更新employee数据
        employeeMapper.update(admin);

    }

    // 店铺入驻审核通过
    @Override
    public void pass(ShopAuditLog shopAuditLog) throws MessagingException {
        Shop shop = shopMapper.loadById(shopAuditLog.getShop_id());
        // 更改店铺审核状态
        shop.setState(1);
        shopMapper.update(shop);
        // 添加日志信息
        shopAuditLog.setState(2);
        // 写死店铺审核人员
        shopAuditLog.setAudit_id(1L);
        // 发送邮箱
        MimeMessage mimeMessage = javaMailSender.createMimeMessage();
        // 创建邮箱工具类接收参数
        MimeMessageHelper helper = new MimeMessageHelper(mimeMessage,true,"utf-8");
        helper.setFrom("1120707667@qq.com");
        helper.setSubject("店铺激活邮件");
        helper.setText("<h1>你的店铺已经注册!!!</h1><a href='http://localhost:8080/shop/active/"+shopAuditLog.getShop_id()+"'>点击该链接激活</a>",true);
        // 获取收件人
//        Employee employee = employeeMapper.loadById(shop.getAdmin_id());
//        String email = employee.getEmail();
        helper.setTo("1520931171@qq.com");
        // 发送
        javaMailSender.send(mimeMessage);

    }


    // 店铺入驻审核驳回
    @Override
    public void reject(ShopAuditLog shopAuditLog) throws MessagingException {
        Shop shop = shopMapper.loadById(shopAuditLog.getShop_id());
        // 更改店铺审核状态
        shop.setState(-1);
        shopMapper.update(shop);
        // 添加日志信息
        shopAuditLog.setState(2);
        // 写死店铺审核人员
        shopAuditLog.setAudit_id(1L);
        // 3. 通知商家   邮件发送
        MimeMessage mimeMessage = javaMailSender.createMimeMessage();
        //发送复杂邮件的工具类
        MimeMessageHelper helper = new MimeMessageHelper(mimeMessage, true, "utf_8");
        helper.setFrom("1120707667@qq.com");
        helper.setSubject("驳回邮件");
        helper.setText("<h1>你的店铺入驻失败!!!</h1><a href='http://localhost:8080/shop/active/"+shopAuditLog.getShop_id()+"'>点击该链接激活</a>",true);

        // 获取收件人
//        Employee employee = employeeMapper.loadById(shop.getAdmin_id());
//        String email = employee.getEmail();
        helper.setTo("1520931171@qq.com");
        // 发送
        javaMailSender.send(mimeMessage);
    }

    @Override
    public HashMap<String, Object> loadShopEchartsData() {
        List<ShopDto> shopEchartVos = shopMapper.loadEcharts();
        List<Integer> states = shopEchartVos.stream().map(ShopDto::getState).collect(Collectors.toList());
        List<Integer> counts = shopEchartVos.stream().map(ShopDto::getCountNum).collect(Collectors.toList());
        HashMap<String, Object> data = new HashMap<>();
        data.put("x", states);
        data.put("y", counts);
        return data;
    }
}
